package com.css.fxfzysx.modules.ysxManagement.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.css.fxfzysx.base.response.RestResponse;
import com.css.fxfzysx.modules.ysxManagement.entity.DynamicFieldsEntity;
import com.css.fxfzysx.modules.ysxManagement.entity.HaManageEntity;
import com.css.fxfzysx.modules.ysxManagement.entity.PdLevelEntity;
import com.css.fxfzysx.modules.ysxManagement.entity.PvRangeEntity;
import com.css.fxfzysx.modules.ysxManagement.repository.PdLevelRepository;
import com.css.fxfzysx.modules.ysxManagement.repository.ZqtCiRepository;
import com.css.fxfzysx.modules.ysxManagement.service.PdLevelService;
import com.css.fxfzysx.modules.ysxManagement.service.ZqtCiService;
import com.css.fxfzysx.modules.ysxManagement.vo.HaManageVo;
import com.css.fxfzysx.modules.ysxManagement.vo.PdLevelVo;
import com.css.fxfzysx.modules.ysxManagement.vo.PvRangeVo;
import com.css.fxfzysx.util.PlatformDateUtils;
import com.css.fxfzysx.util.PlatformObjectUtils;
import com.css.fxfzysx.util.PlatformSessionUtils;
import com.css.fxfzysx.util.UUIDGenerator;
import com.css.fxfzysx.web.PlatformSessionContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class PdLevelServiceImpl implements PdLevelService {

    @Autowired
    private PdLevelRepository pdLevelRepository;

    @Autowired
    private ZqtCiService zqtCiService;

    @Autowired
    private ZqtCiRepository zqtCiRepository;

    @Override
    public RestResponse saveOrUpdatePdLevel(PdLevelVo pdLevelVo) {
        try {
            List<PdLevelEntity> pdLevelList = pdLevelVo.getPdLevelList();
            List<DynamicFieldsEntity> dynamicFieldsList = pdLevelVo.getDynamicFieldsList();
            if (PlatformObjectUtils.isNotEmpty(pdLevelList)) {
                pdLevelList.forEach(it -> {
                    if(PlatformObjectUtils.isEmpty(it.getCpdlId())){
                        it.setCpdlId(UUIDGenerator.getUUID());
                        it.setCreateUser(PlatformSessionContext.getUserID());
                        it.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
                        it.setDelFlag("0");
                        it.setDefaultData("1");
                        pdLevelRepository.savePdLevel(it);
                    }else {
                        it.setUpdateUser(PlatformSessionUtils.getUserId());
                        it.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
                        pdLevelRepository.updatePdLevel(it);
                    }
                });
            }
            zqtCiService.saveOrUpdateDynamicFields(dynamicFieldsList);
            return RestResponse.succeed("成功!");
        } catch (Exception e) {
            String errorMessage = "失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
    }

    @Override
    public RestResponse getAllPdLevel(String titleName) {
        try {
            PdLevelVo pdLevelVo = new PdLevelVo();
            List<PdLevelEntity> pdLevelList = pdLevelRepository.getAllPdLevel();
            List<DynamicFieldsEntity> dynamicFields = zqtCiRepository.getDynamicFields(titleName);
            pdLevelVo.setPdLevelList(pdLevelList);
            pdLevelVo.setDynamicFieldsList(dynamicFields);
            return RestResponse.succeed(pdLevelVo);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
    }

    @Override
    public RestResponse deletePdLevelById(String pdLevelIds,String arrangeIds) {
        try {
            if(PlatformObjectUtils.isNotEmpty(pdLevelIds)){
                for (String pdLevelId : pdLevelIds.split(",")) {
                    pdLevelRepository.deletePdLevelById(pdLevelId);
                }
            }
            if(PlatformObjectUtils.isNotEmpty(arrangeIds)){
                for (String arrangeId : arrangeIds.split(",")) {
                    zqtCiRepository.deleteArrangeById(arrangeId);
                    pdLevelRepository.deletePdLevelByProjectId(arrangeId);
                }
            }
            return RestResponse.succeed("删除成功!");
        } catch (Exception e) {
            String errorMessage = "删除失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
    }

    @Override
    public RestResponse saveOrUpdatePvRange(PvRangeVo pvRangeVo) {
        try {
            List<PvRangeEntity> pvRangeList = pvRangeVo.getPvRangeList();
            List<DynamicFieldsEntity> dynamicFieldsList = pvRangeVo.getDynamicFieldsList();
            if (PlatformObjectUtils.isNotEmpty(pvRangeList)) {
                pvRangeList.forEach(it -> {
                    if(PlatformObjectUtils.isEmpty(it.getCpvrId())){
                        it.setCpvrId(UUIDGenerator.getUUID());
                        it.setCreateUser(PlatformSessionContext.getUserID());
                        it.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
                        it.setDelFlag("0");
                        it.setDefaultData("1");
                        pdLevelRepository.savePvRange(it);
                    }else {
                        it.setUpdateUser(PlatformSessionUtils.getUserId());
                        it.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
                        pdLevelRepository.updatePvRange(it);
                    }
                });
            }
            zqtCiService.saveOrUpdateDynamicFields(dynamicFieldsList);
            return RestResponse.succeed("成功!");
        } catch (Exception e) {
            String errorMessage = "失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
    }

    @Override
    public RestResponse getAllPvRange(String titleName) {
        try {
            PvRangeVo pvRangeVo = new PvRangeVo();
            List<PvRangeEntity> pvRangeList = pdLevelRepository.getAllPvRange();
            List<DynamicFieldsEntity> dynamicFields = zqtCiRepository.getDynamicFields(titleName);
            pvRangeVo.setPvRangeList(pvRangeList);
            pvRangeVo.setDynamicFieldsList(dynamicFields);
            return RestResponse.succeed(pvRangeVo);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
    }

    @Override
    public RestResponse deletePvRangeById(String pvRangeIds,String arrangeIds) {
        try {
            if(PlatformObjectUtils.isNotEmpty(pvRangeIds)){
                for (String pvRangeId : pvRangeIds.split(",")) {
                    pdLevelRepository.deletePvRangeById(pvRangeId);
                }
            }
            if(PlatformObjectUtils.isNotEmpty(arrangeIds)){
                for (String arrangeId : arrangeIds.split(",")) {
                    zqtCiRepository.deleteArrangeById(arrangeId);
                    pdLevelRepository.deletePvRangeByProjectId(arrangeId);
                }
            }
            return RestResponse.succeed("删除成功!");
        } catch (Exception e) {
            String errorMessage = "删除失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
    }
}
